package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件分页查询套餐
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult queryPage(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.queryPage(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        SetmealVO setmealVO = setmealMapper.queryById(id);
        setmealVO.setSetmealDishes(setmealDishMapper.getBySetmealId(id));
        return setmealVO;
    }

    /**
     * 更新套餐
     * @param dto
     */
    @Override
    @Transactional
    public void updatebatech(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        List<Long> dishIds = setmealDishes.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
        List<DishVO> dishVOList = dishIds.stream().map(dishId -> dishMapper.queryById(dishId)).collect(Collectors.toList());
        boolean anyDishDisable = dishVOList.stream().anyMatch(dishVO -> dishVO.getStatus().equals(StatusConstant.DISABLE));
        if (anyDishDisable) {
            setmeal.setStatus(StatusConstant.DISABLE);
        }else {
            setmeal.setStatus(StatusConstant.ENABLE);
        }
        setmealMapper.update(setmeal);
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        if (!setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            setmealDishMapper.save(setmealDishes);
        }
    }

    /**
     * 停售起售套餐
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        if (status.equals(StatusConstant.ENABLE)) {
            List<Long> ids = setmealDishMapper.queryBySetmealId(id);
            List<Dish> dishes = dishMapper.queryStatus(ids);
            boolean anyDishDisabled = dishes.stream().anyMatch(dish -> dish.getStatus().equals(StatusConstant.DISABLE));
            if (anyDishDisabled) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        ids.forEach(id -> {
            SetmealVO setmealVO = setmealMapper.queryById(id);
            if (setmealVO.getStatus().equals(StatusConstant.ENABLE)) {
                throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.deleteByIds(ids);
        ids.forEach(id -> setmealDishMapper.deleteBySetmealId(id));

    }

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Transactional
    @Override
    public void save(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        log.info(setmeal.toString());
        BeanUtils.copyProperties(dto, setmeal);
        log.info(setmeal.toString());
        //判断菜品是否含有=
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        List<Long> dishIds = setmealDishes.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
        List<Dish> dishes = dishMapper.queryStatus(dishIds);
        boolean anyDishDisabled = dishes.stream().anyMatch(dish -> dish.getStatus().equals(StatusConstant.DISABLE));
        if (anyDishDisabled) {
            setmeal.setStatus(StatusConstant.DISABLE);
        }
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        //添加菜品关系表
        if (!setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            setmealDishMapper.save(setmealDishes);
        }
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
